﻿using Common;
using Model.CommonEntities;
using Model.StrategyEntities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public sealed partial class AnalysisEngine
    {
        /// <summary>
        /// BBI多空分界线
        /// </summary>
        /// <param name="stocklist"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public static BBIResult BBI(SortedList<string, SingleStockStru> stocklist, int period = 0)
        {
            List<decimal> BBIList = new List<decimal>();
            //时间列表
            List<decimal> TList = new List<decimal>();
            //收盘价列表
            List<decimal> CloseList = new List<decimal>();
            //开盘价列表
            List<decimal> OpenList = new List<decimal>();
            //最高价列表
            List<decimal> HighList = new List<decimal>();
            //最低价列表
            List<decimal> LowList = new List<decimal>();
            //买入基准价
            List<decimal> arg = new List<decimal>();
            //计算均值时，取值向前偏移的长度，最长要计算24日均值，所以要向前再取23个值
            int shift = 23;
            //实际取值长度 = 计算周期长度 + 向前偏移长度
            int newperiod = period + shift;
            //period += 23;
            for (int i = 0; i < newperiod; i++)
            {
                TList.Add(Convert.ToDecimal(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1]));
                CloseList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].close);
                OpenList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].open);
                HighList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].high);
                LowList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].low);
            }

            TList.Reverse();
            TList = InterceptData.CutList(TList, period, 0);
            CloseList.Reverse();
            var M3 = SMA(InterceptData.CutList(CloseList, period, 2), 3).Values;
            var M6 = SMA(InterceptData.CutList(CloseList, period, 5), 6).Values;
            var M12 = SMA(InterceptData.CutList(CloseList, period, 11), 12).Values;
            var M24 = SMA(InterceptData.CutList(CloseList, period, 23), 24).Values;

            CloseList = InterceptData.CutList(CloseList, period, 0);
            OpenList = InterceptData.CutList(OpenList, period, 0);
            HighList = InterceptData.CutList(HighList, period, 0);
            LowList = InterceptData.CutList(LowList, period, 0);

            for (int i = 0; i < TList.Count; i++)
            {
                BBIList.Add((M3[i]+ M6[i] + M12[i] + M24[i])/4);
                arg.Add((CloseList[i] + OpenList[i]) / 2);
            }
                        
            var result = new BBIResult
            {
                StockCode = stocklist.First().Value.code,
                ShortList = CloseList,
                LongList = BBIList,
                TList = TList,
                CloseList = CloseList,
                OpenList = OpenList,
                HighList = HighList,
                LowList = LowList,
                BenchmarkList = CloseList
            };

            return result;
        }
    }
}
